package com.he.controller;

import com.he.annotation.AccessLimit;
import com.he.domain.Result;
import com.he.domain.dto.LoginDTO;
import com.he.domain.dto.RegisterDTO;
import com.he.entity.UserDTO;
import com.he.service.LoginService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/7 15:43
 * @Desc
 */
@Tag(name = "登录")
@RestController
public class LoginController {

    @Resource
    private LoginService loginService;

    @Operation(description = "查看定时任务日志")
    @PostMapping("/login")
    public Result<?> login(@Validated @RequestBody LoginDTO login){
        return loginService.login(login);
    }


    @Operation(description = "用户邮箱注册")
    @PostMapping("/register")
    public Result<?> register(@Validated @RequestBody RegisterDTO register) {
        loginService.register(register);
        return Result.success();
    }

    /**
     * 发送邮箱验证码
     *
     * @return {@link Result<>}
     */
    @AccessLimit(seconds = 60, maxCount = 10)
    @Operation(description = "发送邮箱验证码")
    @GetMapping("/code")
    public Result<?> sendCode(String username) {
        loginService.sendCode(username);
        return Result.success();
    }

    /**
     * 用户退出
     *
     * @return {@link Result<>}
     */
    @Operation(description = "查看定时任务日志")
    @PostMapping("/logout")
    public Result<?> logout() {
        loginService.logout();
        return Result.success();
    }

    @Operation(description = "查看定时任务日志")
    @PutMapping("/forgetPassword")
    public Result<?> updatePassword(@Validated @RequestBody UserDTO userDTO){
        loginService.updatePassword(userDTO);
        return Result.success();
    }
}
